<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      @close="close"
    >
      <el-form ref="taskForm" :model="taskForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="taskForm.name" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务描述" prop="description">
          <el-input v-model="taskForm.description" placeholder="请输入任务描述" />
        </el-form-item>
        <el-form-item label="执行类" prop="jobClass">
          <el-input v-model="taskForm.jobClass" placeholder="请输入执行类" />
        </el-form-item>
        <el-form-item label="执行时间" prop="cronExpression">
          <el-input v-model="taskForm.cronExpression" placeholder="请输入执行时间">
            <el-button slot="append" icon="el-icon-alarm-clock" @click="showCronDialog" />
          </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button plain @click="resetForm('taskForm')">重 置</el-button>
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('taskForm')">确 定</el-button>
      </span>
    </el-dialog>
    <cron-dialog
      ref="cronRef"
      :expression="taskForm.cronExpression"
      @generateCron="generateCron"
    />
  </div>
</template>
<script>
import CronDialog from './CronDialog.vue'
export default {
  components: {
    CronDialog
  },
  computed: {
    title: function() {
      if (this.taskForm.id === '') {
        return '添加任务'
      } else {
        return '修改任务'
      }
    }
  },
  props: {
    taskForm: {
      type: Object,
      default: () => {
        return {
          id: '',
          name: '',
          description: '',
          jobClass: '',
          cronExpression: ''
        }
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入任务描述', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        jobClass: [
          { required: true, message: '请输入执行类', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        cronExpression: [
          { required: true, message: '请输入执行时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    showCronDialog() {
      this.$refs.cronRef.showCron = true
    },
    generateCron(cronValue) {
      this.taskForm.cronExpression = cronValue
    },
    close() {
      this.resetForm('taskForm')
    },
    closeDialog() {
      this.dialogVisible = false
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submitTaskForm', this.taskForm)
        } else {
          return false
        }
      })
    }
  }
}
</script>
